/*
 * 标签
 */

import { createAPI } from '@/utils/request'

// 标签列表
// export const list = data => createAPI('/tags', 'get', data)
export const getTagsList = function(data) {
  const stateMapping = new Map([
    [1, '已启用'],
    [0, '已禁用'],
  ])
  return createAPI('/tags', 'get', data)
  .then(res => ({
    counts: res.data.counts,
    items: res.data.items.map((d, i) => ({
      ...d,
      addDate: d.addDate.substring(0, 19).replace('T', ' '),
      index: (data.page - 1) * data.pagesize + i + 1,
      state: stateMapping.get(d.state),
    })),
  }))
  .catch(err => err)
}
// 标签简单列表
export const getSimpleList = () => createAPI('/subjects/simple', 'get')

// 标签详情
export const getTagsDetail = data => createAPI(`/tags/${data.id}`, 'get', data)

// 标签添加
export const addTags = data => createAPI('/tags', 'post', data)

// 标签修改
export const updateTags= data => createAPI(`/tags/${data.id}`, 'put', data)

// 标签删除
export const removeTags = data => createAPI(`/tags/${data.id}`, 'delete', data)

// 标签状态
export const changeState = data => createAPI(`/tags/${data.id}/${data.state}`, 'post', data).then(res => res.success).catch(err => Promise.reject(err.response.message))

// 根据id获取标签列表
export const getTagsSimpleList = data => createAPI('/tags/simple', 'get', data)
